package com.zivwu.library.widget.progress;


import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.View;

import com.zivwu.library.R;
import com.zivwu.library.utils.DisplayUtil;

public class CircleProgress extends View {
    /**
     * 默认的环的半径
     */
    private static final int DEF_RING_RADIUS_DP = 20;

    /**
     * 环形进度
     */
    private int progress;
    /**
     * 边框宽度
     */
    private float strokeWidth;
    /**
     * 整个圆环的半径
     */
    private float mRingRadius;

    /**
     * 前环的画笔
     */
    private Paint mForegroundPaint;
    /**
     * 字体画笔
     */
    private Paint mTextPaint;
    /**
     * 后环画笔
     */
    private Paint mBackgroundPaint;
    /**
     * 前景环所需要绘制的矩形区域
     */
    private RectF oval;
    /**
     * 用于计算字体宽高的矩形
     */
    private Rect bounds;

    /**
     * view的中心点
     */
    private int centerX;
    private int centerY;


    public CircleProgress(Context context) {
        super(context);
        init(context, null);
    }

    public CircleProgress(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }


    private void init(Context context, @Nullable AttributeSet attrs) {
        mRingRadius = DisplayUtil.dip2px(getContext(), DEF_RING_RADIUS_DP);
        int backgroundPaintColor = ContextCompat.getColor(getContext(), android.R.color.darker_gray);
        int foregroundPaintColor = ContextCompat.getColor(getContext(), android.R.color.holo_red_light);


        TypedArray typedArray = context.getTheme().
                obtainStyledAttributes(attrs, R.styleable.CircleProgress, 0, 0);
        try {
            backgroundPaintColor = typedArray.getColor(R.styleable.CircleProgress_backgroundColor, backgroundPaintColor);
            foregroundPaintColor = typedArray.getColor(R.styleable.CircleProgress_foregroundColor, foregroundPaintColor);
            mRingRadius = typedArray.getDimensionPixelSize(R.styleable.CircleProgress_ringRadius, (int) mRingRadius);
            progress = typedArray.getInt(R.styleable.CircleProgress_progress, progress);
        } finally {
            typedArray.recycle();
        }


        strokeWidth = mRingRadius / DEF_RING_RADIUS_DP;
        mBackgroundPaint = new Paint();
        setCirclePaint(mBackgroundPaint, strokeWidth, backgroundPaintColor);
        mForegroundPaint = new Paint(mBackgroundPaint);
        mForegroundPaint.setColor(foregroundPaintColor);
        mTextPaint = new Paint();
        mTextPaint.setTextSize(mRingRadius / 2);
        mTextPaint.setAntiAlias(true);
        bounds = new Rect();
        oval = new RectF();

    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        centerX = w / 2;
        centerY = h / 2;

        oval.left = centerX - mRingRadius;
        oval.top = centerY - mRingRadius;
        oval.right = centerX + mRingRadius;
        oval.bottom = centerY + mRingRadius;

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        //处理wrap_content
        int defHeight = (int) ((mRingRadius + strokeWidth) * 2);
        int defWidth = (int) ((mRingRadius + strokeWidth) * 2);
        if (widthMode == MeasureSpec.EXACTLY && heightMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(widthSize, defHeight);
        } else if (widthMode == MeasureSpec.AT_MOST && heightMode == MeasureSpec.EXACTLY) {
            setMeasuredDimension(defWidth, heightSize);
        } else if (widthMode == MeasureSpec.AT_MOST && heightMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(defWidth, defHeight);
        }
    }

    private void setCirclePaint(Paint paint, float strokeWidth, int color) {
        paint.setAntiAlias(true);
        paint.setColor(color);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(strokeWidth);

    }


    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawCircle(centerX, centerY, mRingRadius, mBackgroundPaint);
        canvas.drawArc(oval, -90, progress * 360 / 100, false, mForegroundPaint);
        String text = progress + "%";
        mTextPaint.getTextBounds(text, 0, text.length(), bounds);
        int mTxtHeight = bounds.height();
        float measureText = bounds.width();
        canvas.drawText(text, centerX - measureText / 2, centerY + mTxtHeight / 2, mTextPaint);
    }


    public void onProgress(int progress) {
        ObjectAnimator.ofInt(this, "progress", progress).setDuration(3000).start();
    }

    public int getProgress() {
        return progress;
    }

    private void setProgress(int progress) {
        int ensureProgress = ensureProgress(progress);
        if (this.progress == ensureProgress)
            return;
        this.progress = progress;
        postInvalidate();
    }


    /**
     * 确认progress 在0-100范围之内，并返回
     * @param progress  0-100
     * @return  小于0 返回0，大于100 返回100，其余返回progress
     */
    private int ensureProgress(int progress) {
        if (progress > 100)
            return 100;
        if (progress < 0)
            return 0;
        return progress;

    }

}
